package cn.zzuli.twoapp;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {

    /*protected static final int CHANG_UI = 1;
    protected static final int ERROR = 2;
    private EditText et_path;
    private ImageView ivPic;
    //主线程创建消息处理器
    private Handler handler = new Handler(){
        public void handlerMessage(android.os.Message msg){
            if(msg.what == CHANG_UI){
                Bitmap bitmap = (Bitmap)msg.obj;
                ivPic.setImageBitmap(bitmap);
            }else if(msg.what == ERROR){
                Toast.makeText(MainActivity.this,"显示图片错误",Toast.LENGTH_SHORT).show();
            }
        }
    };*/
    private ImageView logo;
    String strUrl="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png";
    /*private Handler handler = new Handler(){
        public void handlerMessage(Message msg){
            if(msg.what == 11){
                Bitmap bitmap = (Bitmap)msg.obj;
                logo.setImageBitmap(bitmap);
            }
        }
    };*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*setContentView(R.layout.image);
        et_path = (EditText)findViewById(R.id.et_path);
        ivPic = (ImageView)findViewById(R.id.iv_pic);*/
        logo = findViewById(R.id.imageView);
        new Thread(){
            public void run(){
                downPic(strUrl);
            }
        }.start();
    }

    /*public void click(View view){
        final String path = et_path.getText().toString().trim();
        if(TextUtils.isEmpty(path)){
            Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show();
        }else{
            //子线程请求网络，Android 4.0以后访问网络不能放在主线程中
            new Thread(){
                private HttpURLConnection conn;
                private Bitmap bitmap;
                public void run(){
                    //连接服务器GET请求获取图片
                    try{
                        //创建URL对象
                        URL url = new URL(path);
                        //根据URL发送HTTP请求
                        conn = (HttpURLConnection) url.openConnection();
                        //设置请求的方式
                        conn.setRequestMethod("GET");
                        //设置超时时间
                        conn.setConnectTimeout(5000);
                        //得到服务器返回的响应码
                        int code = conn.getResponseCode();
                        //请求网络返回成功后返回码是200
                        if(code == 200){
                            //获取输入流
                            InputStream is = conn.getInputStream();
                            //将流换成Bitmap对象
                            bitmap = BitmapFactory.decodeStream(is);
                            //将更改主界面的消息发送给主线程
                            Message msg = new Message();
                            msg.what = CHANG_UI;
                            msg.obj = bitmap;
                            handler.sendMessage(msg);
                        }else{
                            //返回码不等于200请求服务器失败
                            Message msg = new Message();
                            msg.what = ERROR;
                            handler.sendMessage(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Message msg = new Message();
                        msg.what = ERROR;
                        handler.sendMessage(msg);
                    }
                    //关闭连接
                    conn.disconnect();
                }
            }.start();
        }
    }*/

    private void downPic(String strUrl){
        try{
            URL url = new URL(strUrl);
            HttpURLConnection cn = (HttpURLConnection) url.openConnection();
            if(cn.getResponseCode() == 200){
                InputStream inputStream = cn.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                /*Message msg = new Message();
                msg.what = 11;
                msg.obj = bitmap;
                handler.sendMessage(msg);*/
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        logo.setImageBitmap(bitmap);
                    }
                });
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}